package com.qbw.app.module.vehicle.adapter;

import android.content.Context;
import android.view.ViewGroup;

import androidx.recyclerview.widget.RecyclerView;

import com.qbw.app.model.ElectricArea;
import com.qbw.app.model.EventType;
import com.qbw.app.model.Problem;
import com.qbw.app.model.Travel;
import com.qbw.app.model.TravelChart;
import com.qbw.app.model.Vehicle;
import com.qbw.app.model.Warning;
import com.qbw.app.module.home.holder.HomeTravelHolder;
import com.qbw.app.module.vehicle.holder.VehicleElectricAreaItemHolder;
import com.qbw.app.module.vehicle.holder.VehicleElectricAreaModeItemHolder;
import com.qbw.app.module.vehicle.holder.VehicleEventSubTypeHolder;
import com.qbw.app.module.vehicle.holder.VehicleEventTypeHolder;
import com.qbw.app.module.vehicle.holder.VehicleManageItemHolder;
import com.qbw.app.module.vehicle.holder.VehicleNotificationItemBoxHolder;
import com.qbw.app.module.vehicle.holder.VehicleNotificationItemHolder;
import com.qbw.app.module.vehicle.holder.VehicleNotificationVehicleItemHolder;
import com.qbw.app.module.vehicle.holder.VehicleProblemCategoryHolder;
import com.qbw.app.module.vehicle.holder.VehicleProblemItemHolder;
import com.qbw.app.module.vehicle.holder.VehicleSelectItemHolder;
import com.qbw.app.module.vehicle.holder.VehicleTravekCharHolder;
import com.qbw.app.module.vehicle.holder.VehicleTravelDetailMapHolder;
import com.qbw.app.module.vehicle.holder.VehicleTravelSpeedChartHolder;
import com.qbw.app.module.vehicle.holder.VehicleWarningHolder;

import static com.qbw.app.module.vehicle.adapter.VehicleViewType.VT_ALARM_ITEM;
import static com.qbw.app.module.vehicle.adapter.VehicleViewType.VT_ELECTRIC_AREA_ITEM;
import static com.qbw.app.module.vehicle.adapter.VehicleViewType.VT_EVENT_TYPE;
import static com.qbw.app.module.vehicle.adapter.VehicleViewType.VT_PROBLEM_CATEGORY_GROUP;
import static com.qbw.app.module.vehicle.adapter.VehicleViewType.VT_PROBLEM_ITEM;
import static com.qbw.app.module.vehicle.adapter.VehicleViewType.VT_SUB_EVENT_TYPE;
import static com.qbw.app.module.vehicle.adapter.VehicleViewType.VT_TRAVEL_ITEM;
import static com.qbw.app.module.vehicle.adapter.VehicleViewType.VT_TRAVEL_MAP;
import static com.qbw.app.module.vehicle.adapter.VehicleViewType.VT_TRAVEL_SPEED_CHART;
import static com.qbw.app.module.vehicle.adapter.VehicleViewType.VT_VEHICLE;

/**
 * @author QBW
 * @date 2019-08-24
 */
public class VehicleAdapter extends BaseVehicleAdapter {

    public VehicleAdapter(Context context) {
        super(context);
    }

    public VehicleAdapter(Context context, Object listener) {
        super(context, listener);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder viewHolder;
        switch (viewType) {
            case VT_PROBLEM_ITEM:
                viewHolder = new VehicleProblemItemHolder(mContext, parent);
                break;
            case VT_PROBLEM_CATEGORY_GROUP:
                viewHolder = new VehicleProblemCategoryHolder(mContext, parent, mListener);
                break;
            case VT_ALARM_ITEM:
                viewHolder = new VehicleWarningHolder(mContext, parent);
                break;
            case VT_EVENT_TYPE:
                viewHolder = new VehicleEventTypeHolder(mContext, parent, mListener);
                break;
            case VT_SUB_EVENT_TYPE:
                viewHolder = new VehicleEventSubTypeHolder(mContext, parent, mListener);
                break;
            case VT_TRAVEL_ITEM:
                viewHolder = new HomeTravelHolder(mContext, parent, mListener);
                break;
            case VT_TRAVEL_SPEED_CHART:
                viewHolder = new VehicleTravelSpeedChartHolder(mContext, parent);
                break;
            case VT_TRAVEL_MAP:
                viewHolder = new VehicleTravelDetailMapHolder(mContext, parent);
                break;
            case VT_VEHICLE:
                viewHolder = new VehicleManageItemHolder(mContext, parent);
                break;
            case VT_ELECTRIC_AREA_ITEM:
                viewHolder = new VehicleElectricAreaItemHolder(mContext, parent);
                break;
            case VehicleViewType.VT_NOTIFICATION_VEHICLE_ITEM:
                viewHolder = new VehicleNotificationVehicleItemHolder(mContext, parent);
                break;
            case VehicleViewType.VT_NOTIFICATION_ITEM:
                viewHolder = new VehicleNotificationItemHolder(mContext, parent);
                break;
            case VehicleViewType.VT_NOTIFICATION_ITEM_BOX:
                viewHolder = new VehicleNotificationItemBoxHolder(mContext, parent);
                break;
            case VehicleViewType.VT_SELECT_VEHICLE:
                viewHolder = new VehicleSelectItemHolder(mContext, parent);
                break;
            case VehicleViewType.VT_WARNING_MODE:
                viewHolder = new VehicleElectricAreaModeItemHolder(mContext, parent);
                break;
            case VehicleViewType.VT_TRAVEL_CHART:
                viewHolder = new VehicleTravekCharHolder(mContext,parent);
                break;
            default:
                viewHolder = super.onCreateViewHolder(parent, viewType);
                break;
        }
        return viewHolder;
    }

    @Override
    public int getItemViewType(Object t) {
        if (t instanceof Problem) {
            return VT_PROBLEM_ITEM;
        } else if (t instanceof VehicleProblemCategoryHolder.Data) {
            return VT_PROBLEM_CATEGORY_GROUP;
        } else if (t instanceof Warning) {
            return VT_ALARM_ITEM;
        } else if (t instanceof EventType.SubEventType) {
            return VT_SUB_EVENT_TYPE;
        } else if (t instanceof EventType) {
            return VT_EVENT_TYPE;
        } else if (t instanceof Travel) {
            return VT_TRAVEL_ITEM;
        } else if (t instanceof VehicleTravelDetailMapHolder.Data) {
            return VT_TRAVEL_MAP;
        } else if (t instanceof VehicleTravelSpeedChartHolder.Data) {
            return VT_TRAVEL_SPEED_CHART;
        } else if (t instanceof Vehicle) {
            return VehicleViewType.VT_VEHICLE;
        } else if (t instanceof ElectricArea) {
            return VehicleViewType.VT_ELECTRIC_AREA_ITEM;
        } else if (t instanceof ElectricArea.Mode) {
            return VehicleViewType.VT_WARNING_MODE;
        } else if (t instanceof Warning.Box) {
            return VehicleViewType.VT_NOTIFICATION_ITEM_BOX;
        }else if(t instanceof TravelChart){
            return VehicleViewType.VT_TRAVEL_CHART;
        }
        return super.getItemViewType(t);
    }

    public boolean deleteElectricArea(long id) {
        int c = getItemCount();
        Object o;
        for (int i = 0; i < c; i++) {
            o = getItem(i);
            if (o instanceof ElectricArea && ((ElectricArea) o).getFenceId() == id) {
                removeItem(i);
                return true;
            }
        }
        return false;
    }
}
